/*
 * Copyright (c) 2020-2099 the original author or authors. All rights reserve.
 */
package org.tianyun.cloud.config.refresh.smart;

import org.springframework.util.CollectionUtils;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * 配置刷新器
 *
 * @auther ebert_chan
 */
public class SmartRefresher {

    private SmartScope smartScope;

    private final Set<String> changedKeys = new CopyOnWriteArraySet<>();

    public SmartRefresher(SmartScope smartScope) {
        this.smartScope = smartScope;
    }

    public void addChangedKeys(Set<String> keys) {
        if (CollectionUtils.isEmpty(keys)) {
            return;
        }
        changedKeys.addAll(keys);
    }

    public void refreshOfChangedKeys() {
        Set<String> keys = new HashSet<>(changedKeys);
        changedKeys.clear();
        smartScope.refresh(keys);
    }

    public void refreshAll() {
        smartScope.refreshAll();
    }

    public void refresh(Set<String> keys) {
        smartScope.refresh(keys);
    }

}
